package com.wx.activitys.admin.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wx.activitys.base.utils.BaseUtils;
import com.wx.activitys.base.utils.SMSUtil;
import com.wx.activitys.drp.JsonResult;
import com.wx.activitys.drp.model.BAdmin;
import com.wx.activitys.drp.model.BResource;
import com.wx.activitys.drp.service.AdminService;
import com.wx.activitys.drp.service.BResourceService;
import com.wx.activitys.drp.service.BRoleResourceService;

@Controller
@RequestMapping("/devops")
public class AdminController {
	@Autowired
	private AdminService adminService;
	@Autowired
	private BResourceService bResourceService;
	@Autowired
	private BRoleResourceService bRoleResourceService;

	@RequestMapping(value = "index.html", method = RequestMethod.GET)
	public String share(Model model, HttpServletRequest request, HttpSession session) {
		BAdmin bAdmin = (BAdmin) session.getAttribute("admin");
		List<BResource> bResources = bResourceService.linkselectbyAdId(bAdmin.getAdId());
		model.addAttribute("bResources", bResources);
		return "admin/index";
	}

	/**
	 * 登录
	 */
	@RequestMapping(value = "/login.html", method = RequestMethod.GET)
	public String to(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		return "admin/login";

	}

	/**
	 * 登出
	 */
	@RequestMapping(value = "/logout.html", method = RequestMethod.GET)
	public String out(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		session.setAttribute("admin", null);
		return "admin/login";

	}

	/**
	 * 登录
	 */
	@RequestMapping(value = "/doLogin.html", method = RequestMethod.POST)
	public String toSg(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response,
			@RequestParam("name") String name, @RequestParam("password") String password) throws Exception {
		Map<String, Object> parameter = new HashMap<String, Object>();
		parameter.put("adUser", name);
		parameter.put("adPassword", password);
		parameter.put("adDeleteState", 1);

		List<BAdmin> tag = adminService.selectAdminByMap(parameter);
		// 判断
		if (tag != null && tag.size() >= 1) {
			BAdmin admin = tag.get(0);
			String tempPhone=admin.getAdPhone(); model.addAttribute("phone",
			  tempPhone.substring(0, 4)+"****"+tempPhone.substring(7, 11)); int
			  VCode = BaseUtils.getRandNum(); session.setAttribute("M_VCode",VCode+""); System.out.println(VCode); 
			  SMSUtil.sendSMS(tempPhone,"【嘉华金融】后台管理中心登录验证码为："+VCode); session.setAttribute("mobile",tempPhone);
		      return "admin/mobile_validate";
			//session.setAttribute("admin", tag.get(0));
			//return "redirect:index.html";
		} else {
			model.addAttribute("errorTag", "error");
			return "admin/login";
		}

	}

	/**
	 * 发送短信接口
	 */
	@RequestMapping(value = { "/validateMCode.html" }, method = RequestMethod.POST)
	@ResponseBody
	public JsonResult validateMCode(HttpServletRequest request, HttpSession session) {
		String mCode = request.getParameter("mCode");
		String sCode = (String) session.getAttribute("M_VCode");

		if (sCode.equals(mCode)) {
			String phone = (String) session.getAttribute("mobile");

			Map<String, Object> parameter = new HashMap<String, Object>();
			parameter.put("adPhone", phone);
			parameter.put("adDeleteState", 1);

			List<BAdmin> admins = adminService.selectAdminByMap(parameter);
			session.setAttribute("admin", admins.get(0));
			return JsonResult.success("1", "验证成功");
		} else {
			return JsonResult.success("2", "验证失败");
		}
	}

	/**
	 * 新增
	 */
	@RequestMapping(value = "/toAdd", method = RequestMethod.GET)
	public String insert(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		return "admin/adminManage/add";

	}

	/**
	 * 新增
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response,
			BAdmin admin) throws Exception {
		Date now = new Date();
		admin.setAdCreateTime(now);
		admin.setAdDeleteState(1);
		int tag = adminService.insertAdmin(admin);
		model.addAttribute("subTag", tag);
		return "admin/adminManage/add";

	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/toUpdate", method = RequestMethod.GET)
	public String toUpdate(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response,
			@RequestParam("adminId") int id) throws Exception {
		BAdmin admin = adminService.selectByPirmaryKey(id);
		model.addAttribute("ad", admin);
		return "admin/adminManage/update";

	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response,
			BAdmin admin) throws Exception {

		int tag = adminService.updateAdminByEntity(admin);
		if (tag == 0) {
			tag = -1;
		}
		model.addAttribute("upTag", tag);
		return "forward: list";
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "/updateState", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult delete(Model model, HttpSession session, Integer state, Integer id) throws Exception {
		BAdmin entity = new BAdmin();
		entity.setAdDeleteState(state);
		entity.setAdId(id);
		int result = adminService.updateAdminByEntity(entity);
		return JsonResult.success("1", result);
	}

	/**
	 * 列表页面
	 */
	@RequestMapping(value = "/list", method = { RequestMethod.GET, RequestMethod.POST })
	public String list(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Map<String, Object> parameter = new HashMap<String, Object>();

		parameter.put("begin", 0);
		parameter.put("size", 15);
		List<BAdmin> adminList = adminService.selectAdminByMap(parameter);

		model.addAttribute("adminList", adminList);
		int planSum = adminService.countAdminByMap(parameter);
		int pageSum = planSum / 15;
		if (planSum % 15 == 0) {

		} else {
			pageSum += 1;
		}
		model.addAttribute("pageNo", 1);
		model.addAttribute("pageSum", pageSum);
		model.addAttribute("planSum", planSum);
		return "admin/adminManage/list";
	}

	/**
	 * 翻页
	 */
	@RequestMapping(value = "/newPage", method = RequestMethod.POST)
	@ResponseBody
	public Object newPage(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response,
			@RequestParam("pageNo") int pageNo) throws Exception {

		Map<String, Object> parameter = new HashMap<String, Object>();
		parameter.clear();
		parameter.put("begin", pageNo * 15);
		parameter.put("size", 15);
		List<BAdmin> adminList = adminService.selectAdminByMap(parameter);

		model.addAttribute("adminList", adminList);
		int planSum = adminService.countAdminByMap(parameter);

		int pageSum = planSum / 15;
		if (planSum % 15 == 0) {

		} else {
			pageSum += 1;
		}
		parameter.clear();
		parameter.put("adminList", adminList);
		parameter.put("pageSum", pageSum);
		parameter.put("pageNo", pageNo);
		parameter.put("count", planSum);
		return parameter;

	}

	/**
	 * 分配资源跳转
	 * 
	 * @param request
	 * @param model
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/resource", method = RequestMethod.GET)
	public String grade(HttpServletRequest request, Model model, @RequestParam("args") int[] ids) throws Exception {
		model.addAttribute("args", ids);
		BResource entity = new BResource();
		entity.setState(1);
		if (ids.length == 1) {
			List<BResource> personList = bResourceService.linkselectbyAdId(ids[0]);
			model.addAttribute("personalList", personList);
		}
		List<BResource> bResources = bResourceService.selectByEntity(entity);
		model.addAttribute("bResources", bResources);
		return "admin/adminManage/resource";
	}

	@RequestMapping(value = "updateGrade", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult update(HttpServletRequest request, Model model, @RequestParam("adId[]") int[] ids,
			@RequestParam("resourceId[]") int[] resourceIds) {
		int flag = bRoleResourceService.updateResource(ids, resourceIds);
		if (flag > 0) {
			return JsonResult.success("操作成功", flag);
		} else {
			return JsonResult.success("操作失败", flag);
		}

	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/updatePassword", method = RequestMethod.POST)
	@ResponseBody
	public int updatePassword(Model model, HttpSession session, HttpServletRequest request,
			HttpServletResponse response, @RequestParam("adId") int id, @RequestParam("newPassword") String password)
			throws Exception {
		int tag = -1;
		BAdmin entity = new BAdmin();
		entity.setAdPassword(password);
		entity.setAdId(id);
		tag = adminService.updateAdminByEntity(entity);
		if (tag == 0) {
			tag = -1;
		} else if (tag == 1) {
			BAdmin admin = (BAdmin) session.getAttribute("admin");
			admin.setAdPassword(password);
			session.setAttribute("admin", admin);
		}
		return tag;
	}

}
